Camera Trap Data Suggest Uneven Predation Risk Across Vegetation Types in a Mixed Farmland Landscape

In this paper written by Laux, Waltert, & Gottschalk, they aim to understand the recent and rapid ground-nesting farmland birds population decline within Germany. In the last few decades, agro-biodiversity has been decreasing due to agricultural intensification (increasing field sizes, monocultures, pesticides, and fertilizers) which has led to a large loss in habitat for farmland bird species. The second major reason for this decline has been predator activity which has been increasing within Europe in recent decades as with the changes in landscape composition & habitat loss, birds are forced to nest in sub-optimal locations that are more exposed and congregated which would be more accessible to predators.

In this study, they decided to focus on the effect of landscape composition on mammalian predator activity as it is the least understood factor. The research questions asked include:

  1. Which are the main predators in farmland?
  2. Are there differences in predator activity between vegetation types?
  3. Which environmental parameters explain spatial variation in predator activity best?
  4. How do predators use flower strips, one of the most popular farmland conservation measures?

Their conservation target was the Grey partridge but the results could be applied to the numerous farmland bird species within the same area faced with similar issues. Five vegetation types were found to be most important to grey partridges: flower strips, hedges, field margins, winter cereal, & rapeseed fields. To analyze predation risk, they used 120 camera traps placed evenly and randomly between the five vegetation types. Predator activity was measured by the number of predator captures at each camera site. Consecutive records of the same species at the same site had to be 10 minutes apart to be considered independent captures, except when the individuals could be identified.

Map of the study area with the villages Diemarden and Nesselröden
For my replication project, I aim to calculate the mean capture rates, standard deviation, and confidence intervals for all the predator species within each vegetation type. Their calculated values were shown in Appendix A, Table A7. Then, I’ll calculate the Kruskal-Wallis rank sum test for predator capture rates (captures/100 camera days) between vegetation types. Their values are shown in Appendix A, Table A10. After the Kruskal Wallis tests, I’ll calculate the post-hoc Dunn’s Test to show the significant relationships between vegetation types for each species. This is shown in Appendix A, Table A11. Lastly, I’ll replication Figure 3 in the paper which shows mean capture rates of all predators in different vegetation types.

Packages

Here is a list of packages I’ve downloaded for this replication project.

library(readr)
library(curl)
## Using libcurl 7.79.1 with LibreSSL/3.3.5
## 
## Attaching package: 'curl'
## The following object is masked from 'package:readr':
## 
##     parse_date
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ purrr     1.0.2
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()    masks stats::filter()
## ✖ dplyr::lag()       masks stats::lag()
## ✖ curl::parse_date() masks readr::parse_date()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(ggpubr)
library(rcompanion)
# these packages below were mentioned within the paper for their statistical analysis 
library(stats)
library(ggplot2)
library(ggeffects)
library(FSA)
## Registered S3 methods overwritten by 'FSA':
##   method       from
##   confint.boot car 
##   hist.boot    car 
## ## FSA v0.9.5. See citation('FSA') if used in publication.
## ## Run fishR() for related website and fishR('IFAR') for related book.
library(knitr)

Data

This is the data provided from the study as part of their open data. Within their study, they also focused on predator activity in flower strips as these habitats are farmland conservation methods of creating more habitats for farmland birds. However, for my replication, I focused on comparing predation activity among all vegetation types which is recorded in the first data set.

# uploading the camera trap data from 2019 - 2020 for predator activity within the landscape analysis; what I mainly used for this project
f <- curl("https://raw.githubusercontent.com/amandafwu/Amandafw-data-replication-assignment/main/camera_trap_data_2019_2020.csv") 
camera_trap_data <- read.csv(f, header = TRUE, sep = ",")
head(camera_trap_data)
##            stationID site study_area Vegetation camera Block   SD Year   xcoord
## 1     D_B1 1 1 1 S19 D_B1          D          B      1     1    1  S19 565309.9
## 2   D_B1 45 42 2 S20 D_B1          D          B     45     2   42  S20 565320.6
## 3   D_B2 15 52 2 S19 D_B2          D          B     15     2   52  S19 567028.4
## 4 D_B2 21 45_4 4 S20 D_B2          D          B     21     4 45_4  S20 567090.9
## 5   D_B3 16 16 1 S19 D_B3          D          B     16     1   16  S19 566879.2
## 6   D_B3 23 13 1 S20 D_B3          D          B     23     1   13  S20 566923.4
##    ycoord Runtime            start              end badger boar cats dog fox
## 1 5705259   29359 03/05/2019 10:58 23/05/2019 20:17      0    0    0   0   3
## 2 5705330   30085 23/05/2020 14:00 13/06/2020 11:25      0    0    0   1   3
## 3 5705717   31702 22/05/2019 09:39 13/06/2019 10:01      0    0    0   0   0
## 4 5705745   33310 06/07/2020 11:30 29/07/2020 14:46      0    0    0   0   0
## 5 5704778   30310 29/04/2019 15:01 20/05/2019 16:11      2    0    0   0   1
## 6 5704707   30149 29/04/2020 11:02 20/05/2020 09:31      0    0    0   0   2
##   marten mouseweasel racoon stoat allpredators allpredators_without_dogs
## 1      0           0      0     0            3                         3
## 2      0           0      1     0            5                         4
## 3      0           0      4     0            4                         4
## 4      0           0      0     0            0                         0
## 5      0           0      0     0            3                         3
## 6      0           0      0     0            2                         2
##   badger_100 boar_100 cats_100  dog_100   fox_100 marten_100 mouseweasel_100
## 1   0.000000        0        0 0.000000 14.714398          0               0
## 2   0.000000        0        0 4.786438 14.359315          0               0
## 3   0.000000        0        0 0.000000  0.000000          0               0
## 4   0.000000        0        0 0.000000  0.000000          0               0
## 5   9.501815        0        0 0.000000  4.750907          0               0
## 6   0.000000        0        0 0.000000  9.552556          0               0
##   racoon_100 stoat_100 allpredators_100 allpredators_without_dogs_100 Wood_Dist
## 1   0.000000         0        14.714398                     14.714398  28.13088
## 2   4.786438         0        23.932192                     19.145754  24.89094
## 3  18.169201         0        18.169201                     18.169201  16.97086
## 4   0.000000         0         0.000000                      0.000000  55.05995
## 5   0.000000         0        14.252722                     14.252722 167.98450
## 6   0.000000         0         9.552556                      9.552556 121.20779
##   Water_Dist Settl_Dist Edge_Dist Road_Dist Wood_Area_500m Ext_Area_500m
## 1   307.9476   303.5005  20.96520  291.6105      0.8395633      3.851003
## 2   362.0653   308.8667  16.35203  305.4915      0.7044505      3.367675
## 3  1614.4632   561.8898  16.03188  663.4407      9.8843545     11.166027
## 4  1613.1177   588.1314  54.18470  599.6700     10.8200915     11.035825
## 5   797.4456   688.0288 148.46746  723.3050      0.5968759     14.448198
## 6   737.9161   608.7512  94.22398  646.4316      0.6019155     12.820733
##   Arable_Area_500m Settl_Area_500m Water_Area_500m Edge_Area_500m
## 1         67.41901        2.077997       0.5869732       4.897318
## 2         68.76887        1.261742       0.4928405       4.841052
## 3         56.91473        0.000000       0.0000000       2.973256
## 4         55.17527        0.000000       0.0000000       2.897807
## 5         74.24721        0.000000       0.0000000       2.068146
## 6         74.37032        0.000000       0.0000000       2.000773
##   Road_Density_500m Border_Length_500m Hab_Div_500m Mean_Field_500m
## 1         0.8791997          13.633692    1.8449409        26383.82
## 2         0.8686759          13.359466    1.7792405        25530.34
## 3         0.0000000          11.499183    1.7672184        32290.03
## 4         0.0000000          11.561598    1.6609915        34963.35
## 5         0.0000000           8.329135    0.7299869        35273.41
## 6         0.0000000           8.440240    1.7079133        38522.96
##   Water_Area_1km Water_Area_2.5km Settl_Area_1km Settl_Area_2.5km
## 1       1.408335        25.831754       7.650294         300.3553
## 2       1.367199        25.801172       8.209545         317.9012
## 3       0.000000         4.478745       6.129926         397.5211
## 4       0.000000         4.633185       6.637099         403.6677
## 5       1.274737         8.141304      10.071298         186.4606
## 6       1.378586         8.823942      13.024378         174.9892
##   Forest_Area_500m Forest_Area_1km Forest_Area_2.5km Arable_Area_1km
## 1                0      0.01870404          34.60664        264.6907
## 2                0      0.00000000          33.65598        262.3371
## 3                0      0.00000000         221.35809        242.6029
## 4                0      0.00000000         229.74535        242.0153
## 5                0      0.00000000         108.95230        238.6725
## 6                0      0.00000000         106.56910        228.2221
##   Arable_Area_2.5km Grass_Area_500m Grass_Area_1km Grass_Area_2.5km
## 1          1290.978       0.0000000       11.02834         123.8657
## 2          1272.610       0.0000000       11.55413         125.3705
## 3          1057.581       5.3428669       24.94951         131.7048
## 4          1040.151       6.1343783       24.90011         131.2478
## 5          1317.004       0.4135085       30.52168         155.3072
## 6          1322.714       0.3065722       34.80242         156.7528
f <- curl("https://raw.githubusercontent.com/amandafwu/Amandafw-data-replication-assignment/main/camera_trap_data_flowerstrips_all.csv") # contains camera trap sites, positions (edge or center of the flower strip) and predator observations for each camera site sampled in flower strips
flowerstripsALL <- read.csv(f, header = TRUE, sep = ",")
head(flowerstripsALL)
##   stationID site study_area Position camera      SD Runtime            start
## 1    D D1 c   D1          D        c     32   51_72   18900 03.08.2020 09:44
## 2    D D1 e   D1          D        e     48   12_60    6307 03.08.2020 09:48
## 3   D D10 c  D10          D        c     37       2   32780 03.08.2020 15:28
## 4   D D10 e  D10          D        e     24      73   32776 03.08.2020 15:30
## 5   D D11 c  D11          D        c     20 54_67.2   24410 04.08.2020 11:42
## 6   D D11 e  D11          D        e     33 28.2_16   24684 04.08.2020 11:46
##                end badger boar cats dog fox marten mouseweasel racoon
## 1 26.08.2020 10:08      0    0    0   0   0      0           0      0
## 2 19.08.2020 15:00      0    0    3   0   1      0           0      2
## 3 26.08.2020 09:48      0    0    0   0   0      0           0      0
## 4 26.08.2020 09:46      0    0    1   0   3      0           1      3
## 5 26.08.2020 10:22      0    0    0   0   1      0           0      0
## 6 26.08.2020 10:21      0    0    0   0   0      0           0      1
##   allpredators allpredators_without_dogs badger_100 boar_100  cats_100 dog_100
## 1            0                         0          0        0  0.000000       0
## 2            6                         6          0        0 68.495323       0
## 3            0                         0          0        0  0.000000       0
## 4            8                         8          0        0  4.393459       0
## 5            1                         1          0        0  0.000000       0
## 6            1                         1          0        0  0.000000       0
##     fox_100 marten_100 mouseweasel_100 racoon_100 allpredators_100
## 1  0.000000          0        0.000000   0.000000         0.000000
## 2 22.831774          0        0.000000  45.663548       136.990645
## 3  0.000000          0        0.000000   0.000000         0.000000
## 4 13.180376          0        4.393459  13.180376        35.147669
## 5  5.899222          0        0.000000   0.000000         5.899222
## 6  0.000000          0        0.000000   5.833738         5.833738
##   allpredators_without_dogs_100
## 1                      0.000000
## 2                    136.990645
## 3                      0.000000
## 4                     35.147669
## 5                      5.899222
## 6                      5.833738
f <- curl("https://raw.githubusercontent.com/amandafwu/Amandafw-data-replication-assignment/main/camera_trap_data_flowerstrips_1m.csv") # the same data as above ('camera_trap_data_flowerstrips_all.csv'), but at edge cameras, only predators that passed within 1 m of the camera were included
flowerstrips1m <- read.csv(f, header = TRUE, sep = ",")
head(flowerstrips1m)
##   stationID site study_area Position camera      SD Runtime            start
## 1    D D1 c   D1          D        c     32   51_72   18900 03.08.2020 09:44
## 2    D D1 e   D1          D        e     48   12_60    6307 03.08.2020 09:48
## 3   D D10 c  D10          D        c     37       2   32780 03.08.2020 15:28
## 4   D D10 e  D10          D        e     24      73   32776 03.08.2020 15:30
## 5   D D11 c  D11          D        c     20 54_67.2   24410 04.08.2020 11:42
## 6   D D11 e  D11          D        e     33 28.2_16   24684 04.08.2020 11:46
##                end badger boar cats dog fox marten mouseweasel racoon
## 1 26.08.2020 10:08      0    0    0   0   0      0           0      0
## 2 19.08.2020 15:00      0    0    3   0   1      0           0      2
## 3 26.08.2020 09:48      0    0    0   0   0      0           0      0
## 4 26.08.2020 09:46      0    0    1   0   3      0           1      3
## 5 26.08.2020 10:22      0    0    0   0   1      0           0      0
## 6 26.08.2020 10:21      0    0    0   0   0      0           0      1
##   allpredators allpredators_without_dogs badger_100 boar_100  cats_100 dog_100
## 1            0                         0          0        0  0.000000       0
## 2            6                         6          0        0 68.495323       0
## 3            0                         0          0        0  0.000000       0
## 4            8                         8          0        0  4.393459       0
## 5            1                         1          0        0  0.000000       0
## 6            1                         1          0        0  0.000000       0
##     fox_100 marten_100 mouseweasel_100 racoon_100 allpredators_100
## 1  0.000000          0        0.000000   0.000000         0.000000
## 2 22.831774          0        0.000000  45.663548       136.990645
## 3  0.000000          0        0.000000   0.000000         0.000000
## 4 13.180376          0        4.393459  13.180376        35.147669
## 5  5.899222          0        0.000000   0.000000         5.899222
## 6  0.000000          0        0.000000   5.833738         5.833738
##   allpredators_without_dogs_100
## 1                      0.000000
## 2                    136.990645
## 3                      0.000000
## 4                     35.147669
## 5                      5.899222
## 6                      5.833738

Descriptive Statistic

For my descriptive statistic, I will be replicating Table A7 which calculates the mean capture rates of all predators in each vegetation type. It also calculates the SD and 95% CI.

# first, I calculated the summary stats with one column to see what I need to include in order to get the values. 
all_predators_wodogs <- group_by(camera_trap_data, Vegetation) %>% # grouping the Vegetation types and used piping for the following sections
  summarise(
    mean = mean(allpredators_without_dogs_100, na.rm = TRUE), # calculating mean
    sd = sd(allpredators_without_dogs_100, na.rm = TRUE), # calculating sd
    confint_lower = t.test(allpredators_without_dogs_100, na.rm = TRUE)$conf.int[1], #calculating the lower CI
    confint_upper = t.test(allpredators_without_dogs_100, na.rm = TRUE)$conf.int[2], #calculating the upper CI
    CI_width = ( confint_upper - confint_lower ) / 2 # typically, I would just subtract the upper with lower to get the CI value but this study seemed to have divided the CI values in half in the tables 
  )
all_predators_wodogs # matched the values from table A7 !
## # A tibble: 5 × 6
##   Vegetation  mean    sd confint_lower confint_upper CI_width
##   <chr>      <dbl> <dbl>         <dbl>         <dbl>    <dbl>
## 1 B          19.1   23.2         12.3           25.8     6.75
## 2 E          26.7   35.5         16.3           37.0    10.3 
## 3 G           6.73  15.7          2.17          11.3     4.56
## 4 H          87.9  152.          43.9          132.     44.0 
## 5 R          56.9   55.9         40.7           73.1    16.2
# then, I turned it into a function so I can get the summaries from each column more efficiently
summary_stats <- function(data, group, column) {
  results <- data %>% 
      group_by({{group}}) %>%
      summarise(
        mean = mean({{column}}, na.rm = TRUE),
        sd = sd({{column}}, na.rm = TRUE),
        confint_lower = t.test({{column}}, na.rm = TRUE)$conf.int[1],
        confint_upper = t.test({{column}}, na.rm = TRUE)$conf.int[2],
        CI_width = (confint_upper - confint_lower) / 2
      ) 
}
# this is repetitive but I'm entering all the predator columns into my function; there should be an easier way? 
badger_stats <- summary_stats(camera_trap_data, Vegetation, badger_100)
boar_stats <- summary_stats(camera_trap_data, Vegetation, boar_100)
cats_stats <- summary_stats(camera_trap_data, Vegetation, cats_100)
dog_stats <- summary_stats(camera_trap_data, Vegetation, dog_100)
fox_stats <- summary_stats(camera_trap_data, Vegetation, fox_100)
marten_stats <- summary_stats(camera_trap_data, Vegetation, marten_100)
mouseweasel_stats <- summary_stats(camera_trap_data, Vegetation, mouseweasel_100)
racoon_stats <- summary_stats(camera_trap_data, Vegetation, racoon_100) 
stoat_stats <- summary_stats(camera_trap_data, Vegetation, stoat_100)
# binding all the rows together 
table <- bind_rows(
  all_predators_wodogs,
  badger_stats,
  boar_stats,
  cats_stats,
  dog_stats,
  fox_stats,
  marten_stats,
  mouseweasel_stats,
  racoon_stats,
  stoat_stats
  )
table 
## # A tibble: 50 × 6
##    Vegetation   mean     sd confint_lower confint_upper CI_width
##    <chr>       <dbl>  <dbl>         <dbl>         <dbl>    <dbl>
##  1 B          19.1    23.2        12.3            25.8     6.75 
##  2 E          26.7    35.5        16.3            37.0    10.3  
##  3 G           6.73   15.7         2.17           11.3     4.56 
##  4 H          87.9   152.         43.9           132.     44.0  
##  5 R          56.9    55.9        40.7            73.1    16.2  
##  6 B           2.21    4.76        0.831           3.60    1.38 
##  7 E           4.64    7.49        2.46            6.81    2.17 
##  8 G           0.747   2.44        0.0377          1.46    0.709
##  9 H          12.1    31.0         3.07           21.0     8.99 
## 10 R           8.47   13.2         4.64           12.3     3.83 
## # ℹ 40 more rows
# ordering all the rows for each vegetation type together
ordered1 <- table[order(table$Vegetation), ]
ordered1
## # A tibble: 50 × 6
##    Vegetation    mean     sd confint_lower confint_upper CI_width
##    <chr>        <dbl>  <dbl>         <dbl>         <dbl>    <dbl>
##  1 B          19.1    23.2         12.3           25.8      6.75 
##  2 B           2.21    4.76         0.831          3.60     1.38 
##  3 B           2.74   11.1         -0.493          5.97     3.23 
##  4 B           1.15    3.86         0.0336         2.27     1.12 
##  5 B           0.290   1.48        -0.139          0.719    0.429
##  6 B          10.4    15.0          5.99          14.7      4.37 
##  7 B           0       0          NaN            NaN      NaN    
##  8 B           0.261   1.81        -0.264          0.785    0.524
##  9 B           2.27    7.65         0.0521         4.50     2.22 
## 10 B           0.0911  0.631       -0.0922         0.274    0.183
## # ℹ 40 more rows
# adding a new column in the data set for the list of predators for each row
ordered1$predatorname <- c("All predators", "Badger","Boar","Cats","Dog","Fox","Marten","Mouse weasel","Racoon","Stoat","All predators", "Badger","Boar","Cats","Dog","Fox","Marten","Mouse weasel","Racoon","Stoat","All predators", "Badger","Boar","Cats","Dog","Fox","Marten","Mouse weasel","Racoon","Stoat","All predators", "Badger","Boar","Cats","Dog","Fox","Marten","Mouse weasel","Racoon","Stoat","All predators", "Badger","Boar","Cats","Dog","Fox","Marten","Mouse weasel","Racoon","Stoat")
ordered1
## # A tibble: 50 × 7
##    Vegetation    mean     sd confint_lower confint_upper CI_width predatorname 
##    <chr>        <dbl>  <dbl>         <dbl>         <dbl>    <dbl> <chr>        
##  1 B          19.1    23.2         12.3           25.8      6.75  All predators
##  2 B           2.21    4.76         0.831          3.60     1.38  Badger       
##  3 B           2.74   11.1         -0.493          5.97     3.23  Boar         
##  4 B           1.15    3.86         0.0336         2.27     1.12  Cats         
##  5 B           0.290   1.48        -0.139          0.719    0.429 Dog          
##  6 B          10.4    15.0          5.99          14.7      4.37  Fox          
##  7 B           0       0          NaN            NaN      NaN     Marten       
##  8 B           0.261   1.81        -0.264          0.785    0.524 Mouse weasel 
##  9 B           2.27    7.65         0.0521         4.50     2.22  Racoon       
## 10 B           0.0911  0.631       -0.0922         0.274    0.183 Stoat        
## # ℹ 40 more rows
# changed the order of the columns by making "Vegetation" and "predatorname" to be the first two columns
table1 <- ordered1[, c("Vegetation", "predatorname", setdiff(names(ordered1), c("Vegetation", "predatorname")))]
table1
## # A tibble: 50 × 7
##    Vegetation predatorname     mean     sd confint_lower confint_upper CI_width
##    <chr>      <chr>           <dbl>  <dbl>         <dbl>         <dbl>    <dbl>
##  1 B          All predators 19.1    23.2         12.3           25.8      6.75 
##  2 B          Badger         2.21    4.76         0.831          3.60     1.38 
##  3 B          Boar           2.74   11.1         -0.493          5.97     3.23 
##  4 B          Cats           1.15    3.86         0.0336         2.27     1.12 
##  5 B          Dog            0.290   1.48        -0.139          0.719    0.429
##  6 B          Fox           10.4    15.0          5.99          14.7      4.37 
##  7 B          Marten         0       0          NaN            NaN      NaN    
##  8 B          Mouse weasel   0.261   1.81        -0.264          0.785    0.524
##  9 B          Racoon         2.27    7.65         0.0521         4.50     2.22 
## 10 B          Stoat          0.0911  0.631       -0.0922         0.274    0.183
## # ℹ 40 more rows
# renamed the letters for vegetation type by reassigning the letters to their name 
mapping <- c("B" = "Flower Strips",
             "E" = "Field Margins",
             "H" = "Hedge",
             "R" = "Rapeseed",
             "G" = "Winter Cereal")
table1$Vegetation <- mapping[table1$Vegetation] 
table1 # all the values match the table ! 
## # A tibble: 50 × 7
##    Vegetation   predatorname    mean     sd confint_lower confint_upper CI_width
##    <chr>        <chr>          <dbl>  <dbl>         <dbl>         <dbl>    <dbl>
##  1 Flower Stri… All predato… 19.1    23.2         12.3           25.8      6.75 
##  2 Flower Stri… Badger        2.21    4.76         0.831          3.60     1.38 
##  3 Flower Stri… Boar          2.74   11.1         -0.493          5.97     3.23 
##  4 Flower Stri… Cats          1.15    3.86         0.0336         2.27     1.12 
##  5 Flower Stri… Dog           0.290   1.48        -0.139          0.719    0.429
##  6 Flower Stri… Fox          10.4    15.0          5.99          14.7      4.37 
##  7 Flower Stri… Marten        0       0          NaN            NaN      NaN    
##  8 Flower Stri… Mouse weasel  0.261   1.81        -0.264          0.785    0.524
##  9 Flower Stri… Racoon        2.27    7.65         0.0521         4.50     2.22 
## 10 Flower Stri… Stoat         0.0911  0.631       -0.0922         0.274    0.183
## # ℹ 40 more rows
# to make the final part of the table with all vegetation types for each species, I removed group_by so it would take the mean, SD, and CI from the whole data set, ignoring vegetation type; basically the same function as before
summary_stats2 <- function(data, column) {
  results <- data %>%
      summarise(
        mean = mean({{column}}, na.rm = TRUE),
        sd = sd({{column}}, na.rm = TRUE),
        confint_lower = t.test({{column}}, na.rm = TRUE)$conf.int[1],
        confint_upper = t.test({{column}}, na.rm = TRUE)$conf.int[2],
        CI_width = (confint_upper - confint_lower) / 2
      ) 
}
#repetitive again
allpred_stat_all <- summary_stats2(camera_trap_data, allpredators_without_dogs_100)
badger_stat_all <- summary_stats2(camera_trap_data, badger_100)
boar_stat_all <- summary_stats2(camera_trap_data, boar_100)
cats_stat_all <- summary_stats2(camera_trap_data, cats_100)
dog_stat_all <- summary_stats2(camera_trap_data, dog_100)
fox_stat_all <- summary_stats2(camera_trap_data, fox_100)
marten_stat_all <- summary_stats2(camera_trap_data, marten_100)
mouseweasel_stat_all <- summary_stats2(camera_trap_data, mouseweasel_100)
racoon_stat_all <- summary_stats2(camera_trap_data, racoon_100)
stoat_stat_all <- summary_stats2(camera_trap_data, stoat_100)
# binding all the rows together again
allvegtype_table <- bind_rows(
  allpred_stat_all,
  badger_stat_all,
  boar_stat_all,
  cats_stat_all,
  dog_stat_all,
  fox_stat_all,
  marten_stat_all,
  mouseweasel_stat_all,
  racoon_stat_all,
  stoat_stat_all
  )
allvegtype_table
##           mean         sd confint_lower confint_upper   CI_width
## 1  39.45560936 80.0087922   29.28176800    49.6294507 10.1738414
## 2   5.62530777 16.0160801    3.58871837     7.6618972  2.0365894
## 3   3.67126259 12.2616769    2.11207950     5.2304457  1.5591831
## 4   0.73871238  2.9741362    0.36052407     1.1169007  0.3781883
## 5   1.68523990 11.8639166    0.17663563     3.1938442  1.5086043
## 6  18.81458956 50.4956044   12.39361688    25.2355622  6.4209727
## 7   0.83864506  3.7886519    0.35688371     1.3204064  0.4817614
## 8   0.07111122  0.8581882   -0.03801517     0.1802376  0.1091264
## 9   9.58103680 56.0826268    2.44962371    16.7124499  7.1314131
## 10  0.11494400  0.8317668    0.00917734     0.2207107  0.1057667
# adding the predator name column making sure it aligns with the functions I had entered above
allvegtype_table$predatorname <- c("All predators", "Badger","Boar","Cats","Dog","Fox","Marten","Mouse weasel","Racoon","Stoat") 
# also adding the column for vegetation so when we merge the data sets together, they'll have the same # of columns
allvegtype_table$Vegetation <- c("All vegetation types", "All vegetation types","All vegetation types","All vegetation types","All vegetation types","All vegetation types","All vegetation types","All vegetation types","All vegetation types","All vegetation types") 
# moving the predator name and vegetation column to the front
table2 <- allvegtype_table[, c("Vegetation", "predatorname", setdiff(names(allvegtype_table), c("Vegetation", "predatorname")))]
table2
##              Vegetation  predatorname        mean         sd confint_lower
## 1  All vegetation types All predators 39.45560936 80.0087922   29.28176800
## 2  All vegetation types        Badger  5.62530777 16.0160801    3.58871837
## 3  All vegetation types          Boar  3.67126259 12.2616769    2.11207950
## 4  All vegetation types          Cats  0.73871238  2.9741362    0.36052407
## 5  All vegetation types           Dog  1.68523990 11.8639166    0.17663563
## 6  All vegetation types           Fox 18.81458956 50.4956044   12.39361688
## 7  All vegetation types        Marten  0.83864506  3.7886519    0.35688371
## 8  All vegetation types  Mouse weasel  0.07111122  0.8581882   -0.03801517
## 9  All vegetation types        Racoon  9.58103680 56.0826268    2.44962371
## 10 All vegetation types         Stoat  0.11494400  0.8317668    0.00917734
##    confint_upper   CI_width
## 1     49.6294507 10.1738414
## 2      7.6618972  2.0365894
## 3      5.2304457  1.5591831
## 4      1.1169007  0.3781883
## 5      3.1938442  1.5086043
## 6     25.2355622  6.4209727
## 7      1.3204064  0.4817614
## 8      0.1802376  0.1091264
## 9     16.7124499  7.1314131
## 10     0.2207107  0.1057667
# binding the two data sets together
completetable <- rbind(table1, table2)
completetable
## # A tibble: 60 × 7
##    Vegetation   predatorname    mean     sd confint_lower confint_upper CI_width
##    <chr>        <chr>          <dbl>  <dbl>         <dbl>         <dbl>    <dbl>
##  1 Flower Stri… All predato… 19.1    23.2         12.3           25.8      6.75 
##  2 Flower Stri… Badger        2.21    4.76         0.831          3.60     1.38 
##  3 Flower Stri… Boar          2.74   11.1         -0.493          5.97     3.23 
##  4 Flower Stri… Cats          1.15    3.86         0.0336         2.27     1.12 
##  5 Flower Stri… Dog           0.290   1.48        -0.139          0.719    0.429
##  6 Flower Stri… Fox          10.4    15.0          5.99          14.7      4.37 
##  7 Flower Stri… Marten        0       0          NaN            NaN      NaN    
##  8 Flower Stri… Mouse weasel  0.261   1.81        -0.264          0.785    0.524
##  9 Flower Stri… Racoon        2.27    7.65         0.0521         4.50     2.22 
## 10 Flower Stri… Stoat         0.0911  0.631       -0.0922         0.274    0.183
## # ℹ 50 more rows
# used a function to round all numerical values in the data set to three deciminals to match Table A7
round_df <- function(df, digits) {
  nums <- vapply(df, is.numeric, FUN.VALUE = logical(1))
  df[,nums] <- round(df[,nums], digits = digits)
  (df)
}
finaltable <- round_df(completetable, 3)
finaltable
## # A tibble: 60 × 7
##    Vegetation    predatorname   mean     sd confint_lower confint_upper CI_width
##    <chr>         <chr>         <dbl>  <dbl>         <dbl>         <dbl>    <dbl>
##  1 Flower Strips All predato… 19.1   23.2          12.3          25.8      6.75 
##  2 Flower Strips Badger        2.21   4.76          0.831         3.60     1.38 
##  3 Flower Strips Boar          2.74  11.1          -0.493         5.97     3.23 
##  4 Flower Strips Cats          1.15   3.86          0.034         2.27     1.12 
##  5 Flower Strips Dog           0.29   1.48         -0.139         0.719    0.429
##  6 Flower Strips Fox          10.4   15.0           5.99         14.7      4.37 
##  7 Flower Strips Marten        0      0           NaN           NaN      NaN    
##  8 Flower Strips Mouse weasel  0.261  1.80         -0.264         0.785    0.524
##  9 Flower Strips Racoon        2.27   7.65          0.052         4.50     2.22 
## 10 Flower Strips Stoat         0.091  0.631        -0.092         0.274    0.183
## # ℹ 50 more rows
# omitting the other columns that isn't in the final table
tableA7 <- finaltable[, !names(finaltable) %in% c("confint_lower","confint_upper")]
# using knitr to make it into a table for the html  
kable(tableA7) 
Vegetation predatorname mean sd CI_width
Flower Strips All predators 19.086 23.231 6.746
Flower Strips Badger 2.214 4.761 1.383
Flower Strips Boar 2.737 11.124 3.230
Flower Strips Cats 1.154 3.858 1.120
Flower Strips Dog 0.290 1.477 0.429
Flower Strips Fox 10.356 15.035 4.366
Flower Strips Marten 0.000 0.000 NaN
Flower Strips Mouse weasel 0.261 1.805 0.524
Flower Strips Racoon 2.274 7.651 2.222
Flower Strips Stoat 0.091 0.631 0.183
Field Margins All predators 26.655 35.507 10.310
Field Margins Badger 4.637 7.488 2.174
Field Margins Boar 3.635 17.835 5.179
Field Margins Cats 0.099 0.687 0.199
Field Margins Dog 7.273 25.793 7.489
Field Margins Fox 16.027 25.023 7.266
Field Margins Marten 0.281 1.448 0.420
Field Margins Mouse weasel 0.000 0.000 NaN
Field Margins Racoon 1.681 5.197 1.509
Field Margins Stoat 0.294 1.150 0.334
Winter Cereal All predators 6.728 15.708 4.561
Winter Cereal Badger 0.747 2.443 0.709
Winter Cereal Boar 1.286 3.577 1.039
Winter Cereal Cats 0.000 0.000 NaN
Winter Cereal Dog 0.190 1.315 0.382
Winter Cereal Fox 4.331 13.606 3.951
Winter Cereal Marten 0.000 0.000 NaN
Winter Cereal Mouse weasel 0.000 0.000 NaN
Winter Cereal Racoon 0.364 1.493 0.433
Winter Cereal Stoat 0.000 0.000 NaN
Hedge All predators 87.925 151.615 44.024
Hedge Badger 12.059 30.959 8.990
Hedge Boar 4.796 13.957 4.053
Hedge Cats 1.466 4.194 1.218
Hedge Dog 0.673 2.544 0.739
Hedge Fox 33.177 93.584 27.174
Hedge Marten 3.183 7.687 2.232
Hedge Mouse weasel 0.095 0.658 0.191
Hedge Racoon 32.959 120.515 34.994
Hedge Stoat 0.190 1.316 0.382
Rapeseed All predators 56.884 55.884 16.227
Rapeseed Badger 8.470 13.173 3.825
Rapeseed Boar 5.901 10.101 2.933
Rapeseed Cats 0.975 3.208 0.931
Rapeseed Dog 0.000 0.000 NaN
Rapeseed Fox 30.182 50.327 14.614
Rapeseed Marten 0.729 2.105 0.611
Rapeseed Mouse weasel 0.000 0.000 NaN
Rapeseed Racoon 10.627 24.860 7.219
Rapeseed Stoat 0.000 0.000 NaN
All vegetation types All predators 39.456 80.009 10.174
All vegetation types Badger 5.625 16.016 2.037
All vegetation types Boar 3.671 12.262 1.559
All vegetation types Cats 0.739 2.974 0.378
All vegetation types Dog 1.685 11.864 1.509
All vegetation types Fox 18.815 50.496 6.421
All vegetation types Marten 0.839 3.789 0.482
All vegetation types Mouse weasel 0.071 0.858 0.109
All vegetation types Racoon 9.581 56.083 7.131
All vegetation types Stoat 0.115 0.832 0.106

Inferential Statistic

Performing a Kruskal-Wallis rank sum test of predator capture rates within each vegetation type. A Kruskal-Wallis rank sum test is similar to a one-way ANOVA but for non-parametric data. However, before you conduct a Kruskal-Wallis test, you should first test to see whether the data is normally distributed to determine whether non-parametric tests are necessary. The paper stated that “our data were not normally distributed (Shapiro–Wilk Test, all p < .001, Table A1),” I replicated their Shapiro-Wilk calculations for each predator to compare the results to Table A1.

Shapiro-Wilk Test

# again, this can look repetitive but I wasn't sure how to conduct tests through piping on different columsn within a data set yet
shapiro.test(camera_trap_data$allpredators_without_dogs_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$allpredators_without_dogs_100
## W = 0.45077, p-value < 2.2e-16
shapiro.test(camera_trap_data$badger_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$badger_100
## W = 0.34981, p-value < 2.2e-16
shapiro.test(camera_trap_data$boar_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$boar_100
## W = 0.33097, p-value < 2.2e-16
shapiro.test(camera_trap_data$cats_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$cats_100
## W = 0.27104, p-value < 2.2e-16
shapiro.test(camera_trap_data$dog_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$dog_100
## W = 0.12638, p-value < 2.2e-16
shapiro.test(camera_trap_data$fox_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$fox_100
## W = 0.34682, p-value < 2.2e-16
shapiro.test(camera_trap_data$marten_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$marten_100
## W = 0.23388, p-value < 2.2e-16
shapiro.test(camera_trap_data$mouseweasel_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$mouseweasel_100
## W = 0.057273, p-value < 2.2e-16
shapiro.test(camera_trap_data$racoon_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$racoon_100
## W = 0.14139, p-value < 2.2e-16
shapiro.test(camera_trap_data$stoat_100)
## 
##  Shapiro-Wilk normality test
## 
## data:  camera_trap_data$stoat_100
## W = 0.12136, p-value < 2.2e-16

As seen from the tests, all the p-values are very very low (<0.05) so none of the columns are normally distributed. This should be obvious though as species are hardly ever normally distributed in the wild. If you’d like more information about the Shapiro-Wilk test, my group made a Goodness-of-Fit module with this topic!

Kruskal-Wallis rank sum test

Once it is confirmed that the data is not normally distributed, the Kruskal-Wallis test is used to compare all vegetation types with each predator species and overall with all predators. Their Kruskal-Wallis test values are shown in Appendix A Table A10.

# you put the species first because you are analyzing the species relation to each vegetation type
kruskal.test(allpredators_without_dogs_100 ~ Vegetation, data = camera_trap_data)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  allpredators_without_dogs_100 by Vegetation
## Kruskal-Wallis chi-squared = 78.308, df = 4, p-value = 3.974e-16
kruskal.test(badger_100 ~ Vegetation, data = camera_trap_data)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  badger_100 by Vegetation
## Kruskal-Wallis chi-squared = 29.887, df = 4, p-value = 5.16e-06
kruskal.test(boar_100 ~ Vegetation, data = camera_trap_data)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  boar_100 by Vegetation
## Kruskal-Wallis chi-squared = 18.527, df = 4, p-value = 0.0009733
kruskal.test(cats_100 ~ Vegetation, data = camera_trap_data)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  cats_100 by Vegetation
## Kruskal-Wallis chi-squared = 12.454, df = 4, p-value = 0.01428
kruskal.test(fox_100 ~ Vegetation, data = camera_trap_data)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  fox_100 by Vegetation
## Kruskal-Wallis chi-squared = 37.348, df = 4, p-value = 1.527e-07
kruskal.test(marten_100 ~ Vegetation, data = camera_trap_data)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  marten_100 by Vegetation
## Kruskal-Wallis chi-squared = 22.937, df = 4, p-value = 0.0001304
kruskal.test(mouseweasel_100 ~ Vegetation, data = camera_trap_data)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  mouseweasel_100 by Vegetation
## Kruskal-Wallis chi-squared = 3.0126, df = 4, p-value = 0.5557
kruskal.test(racoon_100 ~ Vegetation, data = camera_trap_data)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  racoon_100 by Vegetation
## Kruskal-Wallis chi-squared = 61.155, df = 4, p-value = 1.658e-12
kruskal.test(stoat_100 ~ Vegetation, data = camera_trap_data)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  stoat_100 by Vegetation
## Kruskal-Wallis chi-squared = 6.1018, df = 4, p-value = 0.1917

To compare, here is the list of KW test results from the paper. The values seem to match exactly!

All of the p-values are also less than 0.05 so they are all significantly different. However, a Kruskal-Wallis test doesn’t tell you which vegetation groups are statistically different so it is important to conduct a post-hoc Dunn’s test to see exactly which factors are significant. Their Dunn’s test values are shown in Appendix A Table A11.

Dunn’s test

# Initally when I performed the Dunn's Test, it would give me the values but it was hard to read because the values in Vegetation weren't named by their actual location so I decided to change the name first for more ease of analysis
names(camera_trap_data) # see the names of columns again
##  [1] "stationID"                     "site"                         
##  [3] "study_area"                    "Vegetation"                   
##  [5] "camera"                        "Block"                        
##  [7] "SD"                            "Year"                         
##  [9] "xcoord"                        "ycoord"                       
## [11] "Runtime"                       "start"                        
## [13] "end"                           "badger"                       
## [15] "boar"                          "cats"                         
## [17] "dog"                           "fox"                          
## [19] "marten"                        "mouseweasel"                  
## [21] "racoon"                        "stoat"                        
## [23] "allpredators"                  "allpredators_without_dogs"    
## [25] "badger_100"                    "boar_100"                     
## [27] "cats_100"                      "dog_100"                      
## [29] "fox_100"                       "marten_100"                   
## [31] "mouseweasel_100"               "racoon_100"                   
## [33] "stoat_100"                     "allpredators_100"             
## [35] "allpredators_without_dogs_100" "Wood_Dist"                    
## [37] "Water_Dist"                    "Settl_Dist"                   
## [39] "Edge_Dist"                     "Road_Dist"                    
## [41] "Wood_Area_500m"                "Ext_Area_500m"                
## [43] "Arable_Area_500m"              "Settl_Area_500m"              
## [45] "Water_Area_500m"               "Edge_Area_500m"               
## [47] "Road_Density_500m"             "Border_Length_500m"           
## [49] "Hab_Div_500m"                  "Mean_Field_500m"              
## [51] "Water_Area_1km"                "Water_Area_2.5km"             
## [53] "Settl_Area_1km"                "Settl_Area_2.5km"             
## [55] "Forest_Area_500m"              "Forest_Area_1km"              
## [57] "Forest_Area_2.5km"             "Arable_Area_1km"              
## [59] "Arable_Area_2.5km"             "Grass_Area_500m"              
## [61] "Grass_Area_1km"                "Grass_Area_2.5km"
camera_trap_data_DT <- camera_trap_data %>%
  mutate(Vegetation = case_when(
    Vegetation == "B" ~ "Flower Strips",
    Vegetation == "E" ~ "Field Margins",
    Vegetation == "H" ~ "Hedge",
    Vegetation == "R" ~ "Rapeseed",
    Vegetation == "G" ~ "Winter Cereal",
    TRUE ~ as.character(Vegetation) # Keep other values unchanged
  ))
head(camera_trap_data_DT)
##            stationID site study_area    Vegetation camera Block   SD Year
## 1     D_B1 1 1 1 S19 D_B1          D Flower Strips      1     1    1  S19
## 2   D_B1 45 42 2 S20 D_B1          D Flower Strips     45     2   42  S20
## 3   D_B2 15 52 2 S19 D_B2          D Flower Strips     15     2   52  S19
## 4 D_B2 21 45_4 4 S20 D_B2          D Flower Strips     21     4 45_4  S20
## 5   D_B3 16 16 1 S19 D_B3          D Flower Strips     16     1   16  S19
## 6   D_B3 23 13 1 S20 D_B3          D Flower Strips     23     1   13  S20
##     xcoord  ycoord Runtime            start              end badger boar cats
## 1 565309.9 5705259   29359 03/05/2019 10:58 23/05/2019 20:17      0    0    0
## 2 565320.6 5705330   30085 23/05/2020 14:00 13/06/2020 11:25      0    0    0
## 3 567028.4 5705717   31702 22/05/2019 09:39 13/06/2019 10:01      0    0    0
## 4 567090.9 5705745   33310 06/07/2020 11:30 29/07/2020 14:46      0    0    0
## 5 566879.2 5704778   30310 29/04/2019 15:01 20/05/2019 16:11      2    0    0
## 6 566923.4 5704707   30149 29/04/2020 11:02 20/05/2020 09:31      0    0    0
##   dog fox marten mouseweasel racoon stoat allpredators
## 1   0   3      0           0      0     0            3
## 2   1   3      0           0      1     0            5
## 3   0   0      0           0      4     0            4
## 4   0   0      0           0      0     0            0
## 5   0   1      0           0      0     0            3
## 6   0   2      0           0      0     0            2
##   allpredators_without_dogs badger_100 boar_100 cats_100  dog_100   fox_100
## 1                         3   0.000000        0        0 0.000000 14.714398
## 2                         4   0.000000        0        0 4.786438 14.359315
## 3                         4   0.000000        0        0 0.000000  0.000000
## 4                         0   0.000000        0        0 0.000000  0.000000
## 5                         3   9.501815        0        0 0.000000  4.750907
## 6                         2   0.000000        0        0 0.000000  9.552556
##   marten_100 mouseweasel_100 racoon_100 stoat_100 allpredators_100
## 1          0               0   0.000000         0        14.714398
## 2          0               0   4.786438         0        23.932192
## 3          0               0  18.169201         0        18.169201
## 4          0               0   0.000000         0         0.000000
## 5          0               0   0.000000         0        14.252722
## 6          0               0   0.000000         0         9.552556
##   allpredators_without_dogs_100 Wood_Dist Water_Dist Settl_Dist Edge_Dist
## 1                     14.714398  28.13088   307.9476   303.5005  20.96520
## 2                     19.145754  24.89094   362.0653   308.8667  16.35203
## 3                     18.169201  16.97086  1614.4632   561.8898  16.03188
## 4                      0.000000  55.05995  1613.1177   588.1314  54.18470
## 5                     14.252722 167.98450   797.4456   688.0288 148.46746
## 6                      9.552556 121.20779   737.9161   608.7512  94.22398
##   Road_Dist Wood_Area_500m Ext_Area_500m Arable_Area_500m Settl_Area_500m
## 1  291.6105      0.8395633      3.851003         67.41901        2.077997
## 2  305.4915      0.7044505      3.367675         68.76887        1.261742
## 3  663.4407      9.8843545     11.166027         56.91473        0.000000
## 4  599.6700     10.8200915     11.035825         55.17527        0.000000
## 5  723.3050      0.5968759     14.448198         74.24721        0.000000
## 6  646.4316      0.6019155     12.820733         74.37032        0.000000
##   Water_Area_500m Edge_Area_500m Road_Density_500m Border_Length_500m
## 1       0.5869732       4.897318         0.8791997          13.633692
## 2       0.4928405       4.841052         0.8686759          13.359466
## 3       0.0000000       2.973256         0.0000000          11.499183
## 4       0.0000000       2.897807         0.0000000          11.561598
## 5       0.0000000       2.068146         0.0000000           8.329135
## 6       0.0000000       2.000773         0.0000000           8.440240
##   Hab_Div_500m Mean_Field_500m Water_Area_1km Water_Area_2.5km Settl_Area_1km
## 1    1.8449409        26383.82       1.408335        25.831754       7.650294
## 2    1.7792405        25530.34       1.367199        25.801172       8.209545
## 3    1.7672184        32290.03       0.000000         4.478745       6.129926
## 4    1.6609915        34963.35       0.000000         4.633185       6.637099
## 5    0.7299869        35273.41       1.274737         8.141304      10.071298
## 6    1.7079133        38522.96       1.378586         8.823942      13.024378
##   Settl_Area_2.5km Forest_Area_500m Forest_Area_1km Forest_Area_2.5km
## 1         300.3553                0      0.01870404          34.60664
## 2         317.9012                0      0.00000000          33.65598
## 3         397.5211                0      0.00000000         221.35809
## 4         403.6677                0      0.00000000         229.74535
## 5         186.4606                0      0.00000000         108.95230
## 6         174.9892                0      0.00000000         106.56910
##   Arable_Area_1km Arable_Area_2.5km Grass_Area_500m Grass_Area_1km
## 1        264.6907          1290.978       0.0000000       11.02834
## 2        262.3371          1272.610       0.0000000       11.55413
## 3        242.6029          1057.581       5.3428669       24.94951
## 4        242.0153          1040.151       6.1343783       24.90011
## 5        238.6725          1317.004       0.4135085       30.52168
## 6        228.2221          1322.714       0.3065722       34.80242
##   Grass_Area_2.5km
## 1         123.8657
## 2         125.3705
## 3         131.7048
## 4         131.2478
## 5         155.3072
## 6         156.7528
# this is an initial test where I calculate the Dunn's test values on badgers first to see if my code and values match 
badgerDT <- dunnTest(badger_100 ~ Vegetation, data = camera_trap_data_DT, method="holm", list = TRUE) # function I found for Dunn's Test
## Warning: Vegetation was coerced to a factor.
badgerDT
## Dunn (1964) Kruskal-Wallis multiple comparison
##   p-values adjusted with the Holm method.
##                       Comparison           Z      P.unadj        P.adj
## 1  Field Margins - Flower Strips  2.10356959 3.541601e-02 0.1770800459
## 2          Field Margins - Hedge -0.85160640 3.944326e-01 1.0000000000
## 3          Flower Strips - Hedge -2.95517598 3.124907e-03 0.0218743521
## 4       Field Margins - Rapeseed -0.83464213 4.039192e-01 0.8078384713
## 5       Flower Strips - Rapeseed -2.93821171 3.301115e-03 0.0198066906
## 6               Hedge - Rapeseed  0.01696427 9.864651e-01 0.9864651194
## 7  Field Margins - Winter Cereal  3.48446124 4.931289e-04 0.0039450311
## 8  Flower Strips - Winter Cereal  1.38089165 1.673123e-01 0.6692491127
## 9          Hedge - Winter Cereal  4.33606763 1.450543e-05 0.0001450543
## 10      Rapeseed - Winter Cereal  4.31910336 1.566644e-05 0.0001409980
str(badgerDT) # seeing how the test outputs are presented so I can manipulate the data
## List of 3
##  $ method: chr "Holm"
##  $ res   :'data.frame':  10 obs. of  4 variables:
##   ..$ Comparison: chr [1:10] "Field Margins - Flower Strips" "Field Margins - Hedge" "Flower Strips - Hedge" "Field Margins - Rapeseed" ...
##   ..$ Z         : num [1:10] 2.104 -0.852 -2.955 -0.835 -2.938 ...
##   ..$ P.unadj   : num [1:10] 0.03542 0.39443 0.00312 0.40392 0.0033 ...
##   ..$ P.adj     : num [1:10] 0.1771 1 0.0219 0.8078 0.0198 ...
##  $ dtres : chr [1:39] "  Kruskal-Wallis rank sum test" "" "data: x and g" "Kruskal-Wallis chi-squared = 29.8871, df = 4, p-value = 0" ...
##  - attr(*, "class")= chr "dunnTest"
new_badgerDT <- badgerDT$res[, !names(badgerDT$res) %in% c("P.unadj")] # within the results, I called the names of the columns and took 
new_badgerDT 
##                       Comparison           Z        P.adj
## 1  Field Margins - Flower Strips  2.10356959 0.1770800459
## 2          Field Margins - Hedge -0.85160640 1.0000000000
## 3          Flower Strips - Hedge -2.95517598 0.0218743521
## 4       Field Margins - Rapeseed -0.83464213 0.8078384713
## 5       Flower Strips - Rapeseed -2.93821171 0.0198066906
## 6               Hedge - Rapeseed  0.01696427 0.9864651194
## 7  Field Margins - Winter Cereal  3.48446124 0.0039450311
## 8  Flower Strips - Winter Cereal  1.38089165 0.6692491127
## 9          Hedge - Winter Cereal  4.33606763 0.0001450543
## 10      Rapeseed - Winter Cereal  4.31910336 0.0001409980
# made a function to get the z-value and adj P to get the other values
dunns_test <- function(data, species) {
  dt_result <- dunnTest(data[[species]] ~ Vegetation, data = data, method = "holm", list = TRUE) # same code as above
    final <- dt_result$res[, !names(dt_result$res) %in% c("P.unadj")] # same code as above
  return(final)
}
# running the function with every column
apdt <- dunns_test(camera_trap_data_DT, "allpredators_without_dogs_100")
## Warning: Vegetation was coerced to a factor.
bdt <- dunns_test(camera_trap_data_DT, "badger_100")
## Warning: Vegetation was coerced to a factor.
bodt <- dunns_test(camera_trap_data_DT, "boar_100")
## Warning: Vegetation was coerced to a factor.
cdt <- dunns_test(camera_trap_data_DT, "cats_100")
## Warning: Vegetation was coerced to a factor.
fdt <- dunns_test(camera_trap_data_DT, "fox_100")
## Warning: Vegetation was coerced to a factor.
mdt <- dunns_test(camera_trap_data_DT, "marten_100")
## Warning: Vegetation was coerced to a factor.
mwdt <- dunns_test(camera_trap_data_DT, "mouseweasel_100")
## Warning: Vegetation was coerced to a factor.
rdt <- dunns_test(camera_trap_data_DT, "racoon_100")
## Warning: Vegetation was coerced to a factor.
sdt <- dunns_test(camera_trap_data_DT, "stoat_100")
## Warning: Vegetation was coerced to a factor.
# binding everything together
tabledt <- bind_rows(apdt, bdt, bodt, cdt, fdt, mdt, mwdt, rdt, sdt)
# making a new column to label each row within the new table
predator <- c("All predators", "All predators", "All predators", "All predators", "All predators", "All predators", "All predators", "All predators", "All predators", "All predators", "badger","badger","badger","badger","badger","badger","badger","badger","badger","badger", "boar", "boar", "boar", "boar", "boar", "boar", "boar", "boar", "boar", "boar", "cats", "cats", "cats", "cats", "cats", "cats", "cats", "cats", "cats", "cats", "fox", "fox", "fox", "fox", "fox", "fox", "fox", "fox", "fox", "fox", "marten", "marten", "marten", "marten", "marten", "marten", "marten", "marten", "marten", "marten", "mouse weasel", "mouse weasel", "mouse weasel", "mouse weasel", "mouse weasel", "mouse weasel", "mouse weasel", "mouse weasel", "mouse weasel", "mouse weasel", "racoon", "racoon", "racoon", "racoon", "racoon", "racoon", "racoon", "racoon", "racoon", "racoon", "stoat","stoat","stoat","stoat","stoat","stoat","stoat","stoat","stoat","stoat")
tabledt$predator <- predator # adding the new column
# reordering it to be in front
A11 <- tabledt[, c("predator", setdiff(names(tabledt), "predator"))]
A11
##         predator                    Comparison            Z        P.adj
## 1  All predators Field Margins - Flower Strips  0.569618963 1.000000e+00
## 2  All predators         Field Margins - Hedge -3.587639017 1.668429e-03
## 3  All predators         Flower Strips - Hedge -4.157257979 2.576722e-04
## 4  All predators      Field Margins - Rapeseed -3.121452810 5.398833e-03
## 5  All predators      Flower Strips - Rapeseed -3.691071773 1.339867e-03
## 6  All predators              Hedge - Rapeseed  0.466186207 6.410822e-01
## 7  All predators Field Margins - Winter Cereal  4.126228153 2.581330e-04
## 8  All predators Flower Strips - Winter Cereal  3.556609190 1.502690e-03
## 9  All predators         Hedge - Winter Cereal  7.713867169 1.220615e-13
## 10 All predators      Rapeseed - Winter Cereal  7.247680963 3.815710e-12
## 11        badger Field Margins - Flower Strips  2.103569587 1.770800e-01
## 12        badger         Field Margins - Hedge -0.851606397 1.000000e+00
## 13        badger         Flower Strips - Hedge -2.955175985 2.187435e-02
## 14        badger      Field Margins - Rapeseed -0.834642127 8.078385e-01
## 15        badger      Flower Strips - Rapeseed -2.938211714 1.980669e-02
## 16        badger              Hedge - Rapeseed  0.016964271 9.864651e-01
## 17        badger Field Margins - Winter Cereal  3.484461236 3.945031e-03
## 18        badger Flower Strips - Winter Cereal  1.380891649 6.692491e-01
## 19        badger         Hedge - Winter Cereal  4.336067633 1.450543e-04
## 20        badger      Rapeseed - Winter Cereal  4.319103363 1.409980e-04
## 21          boar Field Margins - Flower Strips -0.055077903 9.560764e-01
## 22          boar         Field Margins - Hedge -1.192334602 1.000000e+00
## 23          boar         Flower Strips - Hedge -1.137256699 1.000000e+00
## 24          boar      Field Margins - Rapeseed -3.640241399 2.723826e-03
## 25          boar      Flower Strips - Rapeseed -3.585163496 3.031804e-03
## 26          boar              Hedge - Rapeseed -2.447906797 1.005822e-01
## 27          boar Field Margins - Winter Cereal -0.385545321 1.000000e+00
## 28          boar Flower Strips - Winter Cereal -0.330467418 1.000000e+00
## 29          boar         Hedge - Winter Cereal  0.806789282 1.000000e+00
## 30          boar      Rapeseed - Winter Cereal  3.254696079 9.081102e-03
## 31          cats Field Margins - Flower Strips -1.499767495 6.683732e-01
## 32          cats         Field Margins - Hedge -2.571687207 9.108385e-02
## 33          cats         Flower Strips - Hedge -1.071919712 1.000000e+00
## 34          cats      Field Margins - Rapeseed -1.847873038 3.877243e-01
## 35          cats      Flower Strips - Rapeseed -0.348105543 7.277609e-01
## 36          cats              Hedge - Rapeseed  0.723814169 1.000000e+00
## 37          cats Field Margins - Winter Cereal  0.360373580 1.000000e+00
## 38          cats Flower Strips - Winter Cereal  1.860141075 4.400590e-01
## 39          cats         Hedge - Winter Cereal  2.932060787 3.367208e-02
## 40          cats      Rapeseed - Winter Cereal  2.208246618 2.178167e-01
## 41           fox Field Margins - Flower Strips  0.723957417 9.381838e-01
## 42           fox         Field Margins - Hedge -1.018206222 1.000000e+00
## 43           fox         Flower Strips - Hedge -1.742163639 4.888789e-01
## 44           fox      Field Margins - Rapeseed -0.971547844 9.938266e-01
## 45           fox      Flower Strips - Rapeseed -1.695505261 4.498981e-01
## 46           fox              Hedge - Rapeseed  0.046658378 9.627855e-01
## 47           fox Field Margins - Winter Cereal  4.234624102 1.831475e-04
## 48           fox Flower Strips - Winter Cereal  3.510666685 3.128891e-03
## 49           fox         Hedge - Winter Cereal  5.252830324 1.497795e-06
## 50           fox      Rapeseed - Winter Cereal  5.206171945 1.734984e-06
## 51        marten Field Margins - Flower Strips  0.762930457 1.000000e+00
## 52        marten         Field Margins - Hedge -3.167610000 1.229580e-02
## 53        marten         Flower Strips - Hedge -3.930540458 8.475513e-04
## 54        marten      Field Margins - Rapeseed -1.496888872 5.376887e-01
## 55        marten      Flower Strips - Rapeseed -2.259819329 1.668273e-01
## 56        marten              Hedge - Rapeseed  1.670721128 4.738839e-01
## 57        marten Field Margins - Winter Cereal  0.762930457 8.910097e-01
## 58        marten Flower Strips - Winter Cereal  0.000000000 1.000000e+00
## 59        marten         Hedge - Winter Cereal  3.930540458 7.627962e-04
## 60        marten      Rapeseed - Winter Cereal  2.259819329 1.429948e-01
## 61  mouse weasel Field Margins - Flower Strips -1.125038741 1.000000e+00
## 62  mouse weasel         Field Margins - Hedge -1.115702320 1.000000e+00
## 63  mouse weasel         Flower Strips - Hedge  0.009336421 1.000000e+00
## 64  mouse weasel      Field Margins - Rapeseed  0.000000000 1.000000e+00
## 65  mouse weasel      Flower Strips - Rapeseed  1.125038741 1.000000e+00
## 66  mouse weasel              Hedge - Rapeseed  1.115702320 1.000000e+00
## 67  mouse weasel Field Margins - Winter Cereal  0.000000000 1.000000e+00
## 68  mouse weasel Flower Strips - Winter Cereal  1.125038741 1.000000e+00
## 69  mouse weasel         Hedge - Winter Cereal  1.115702320 1.000000e+00
## 70  mouse weasel      Rapeseed - Winter Cereal  0.000000000 1.000000e+00
## 71        racoon Field Margins - Flower Strips -0.083044499 9.338162e-01
## 72        racoon         Field Margins - Hedge -5.152369577 2.314939e-06
## 73        racoon         Flower Strips - Hedge -5.069325078 2.794602e-06
## 74        racoon      Field Margins - Rapeseed -4.229853510 1.403061e-04
## 75        racoon      Flower Strips - Rapeseed -4.146809011 1.685705e-04
## 76        racoon              Hedge - Rapeseed  0.922516066 1.000000e+00
## 77        racoon Field Margins - Winter Cereal  0.899046969 7.372553e-01
## 78        racoon Flower Strips - Winter Cereal  0.982091468 1.000000e+00
## 79        racoon         Hedge - Winter Cereal  6.051416546 1.435777e-08
## 80        racoon      Rapeseed - Winter Cereal  5.128900479 2.331515e-06
## 81         stoat Field Margins - Flower Strips  1.437981645 1.000000e+00
## 82         stoat         Field Margins - Hedge  1.414213353 1.000000e+00
## 83         stoat         Flower Strips - Hedge -0.023768292 1.000000e+00
## 84         stoat      Field Margins - Rapeseed  2.139146249 3.242383e-01
## 85         stoat      Flower Strips - Rapeseed  0.701164604 1.000000e+00
## 86         stoat              Hedge - Rapeseed  0.724932895 1.000000e+00
## 87         stoat Field Margins - Winter Cereal  2.139146249 2.918144e-01
## 88         stoat Flower Strips - Winter Cereal  0.701164604 1.000000e+00
## 89         stoat         Hedge - Winter Cereal  0.724932895 1.000000e+00
## 90         stoat      Rapeseed - Winter Cereal  0.000000000 1.000000e+00

All of my values reflect the values from Table A11 which is amazing! From replicating tables A7, A10, and A11, these are some of comparisons they have made:

“The number of predator captures in flower strips (mean 19.09 SD 23.23) was significantly lower than in hedges (mean 87.93, SD 151.62) and rapeseed fields (mean 56.88, SD 55.88) and also less than in field margins, although this difference was not significant (Tables A7, A10, A11). A similar pattern between vegetation types was observed for foxes, although only the differences between winter cereal and the other vegetation types were significant (Tables A7, A10, A11).”

Figure Replication

This is my replication of Figure 3 from the paper, showing mean capture rates of all predators in different vegetation types. Figure 3 is shown below which compared mean capture rate (captures/100days) of “all predators” in different vegetation types.

# used ggplot to make the figure; used reorder with the mean to have the vegetation types reorder by increasing order; "allpredators_without_dogs_100" is the column showing the total number of independent predator captures, excluding dogs, extrapolated to 100 active camera days (according to the README)
p <- ggplot(camera_trap_data, aes(x=reorder(Vegetation, allpredators_without_dogs_100, FUN = mean), y=allpredators_without_dogs_100)) + 
  stat_summary(geom = "point", fun.y = mean, shape = 15, size = 2) + # makes it a scatter plot and played with the sizing and shape to look closest to the original figure
  ylab("captures/100 days") + # renaming y axis
  xlab("vegetation type") + # renaming x axis 
  stat_summary(fun.data = "mean_sdl", fun.args = list(mult = .3), geom = "errorbar", width = 0.15) + # added error bars and manipulated the multitude and width to best reflect the original figure; this might relate to how the CI intervals were also manipulated in the table ? 
  scale_x_discrete(labels = c("G" = "winter cereal", "B" = "flower strip", "E" = "field margin", "R" = "rapeseed", "H" = "hedge")) + # renaming the labels in Vegetation
    geom_text( aes(x = "G", y = 4, label = "b"), vjust = -0.5,
            hjust= -1.5, size = 3) +
    geom_text( aes(x = "B", y = 18, label = "a"), vjust = -0.5,
            hjust= -1.5, size = 3) +
    geom_text( aes(x = "E", y = 25, label = "a"), vjust = -0.5,
            hjust= -1.5, size = 3) +
    geom_text( aes(x = "R", y = 55, label = "c"), vjust = -0.5,
            hjust= -1.5, size = 3) +
    geom_text( aes(x = "H", y = 85, label = "c"), vjust = -0.5,
            hjust= -1.5, size = 3) +
  theme(axis.title = element_text(face = "bold")) # made the axis titles bold
## Warning: The `fun.y` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
## ℹ Please use the `fun` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
p

# to add the post-hoc Dunn's test results into the graph I had to calculate the values first
allpredDT <- dunnTest(allpredators_without_dogs_100 ~ Vegetation, data = camera_trap_data, method="holm", list = TRUE) # function I found for Dunn's Test
## Warning: Vegetation was coerced to a factor.
allpredDT
## Dunn (1964) Kruskal-Wallis multiple comparison
##   p-values adjusted with the Holm method.
##    Comparison          Z      P.unadj        P.adj
## 1       B - E -0.5696190 5.689362e-01 1.000000e+00
## 2       B - G  3.5566092 3.756724e-04 1.502690e-03
## 3       E - G  4.1262282 3.687614e-05 2.581330e-04
## 4       B - H -4.1572580 3.220902e-05 2.576722e-04
## 5       E - H -3.5876390 3.336858e-04 1.668429e-03
## 6       G - H -7.7138672 1.220615e-14 1.220615e-13
## 7       B - R -3.6910718 2.233111e-04 1.339867e-03
## 8       E - R -3.1214528 1.799611e-03 5.398833e-03
## 9       G - R -7.2476810 4.239678e-13 3.815710e-12
## 10      H - R  0.4661862 6.410822e-01 6.410822e-01
test <- allpredDT$res # taking out the results
test
##    Comparison          Z      P.unadj        P.adj
## 1       B - E -0.5696190 5.689362e-01 1.000000e+00
## 2       B - G  3.5566092 3.756724e-04 1.502690e-03
## 3       E - G  4.1262282 3.687614e-05 2.581330e-04
## 4       B - H -4.1572580 3.220902e-05 2.576722e-04
## 5       E - H -3.5876390 3.336858e-04 1.668429e-03
## 6       G - H -7.7138672 1.220615e-14 1.220615e-13
## 7       B - R -3.6910718 2.233111e-04 1.339867e-03
## 8       E - R -3.1214528 1.799611e-03 5.398833e-03
## 9       G - R -7.2476810 4.239678e-13 3.815710e-12
## 10      H - R  0.4661862 6.410822e-01 6.410822e-01
# getting the letters of significance
cld <- cldList(comparison = test$Comparison,
        p.value    = test$P.adj,
        threshold  = 0.05)[1:2]
cld # matches the values from the graph !!
##   Group Letter
## 1     B      a
## 2     E      a
## 3     G      b
## 4     H      c
## 5     R      c

Although I was able to get the CLD letters from the Dunn’s Test, adding it into the graph was unexpectedly difficult as the length of the data in CLD$Letter was only 5 but the length of the whole data set was 240. I couldn’t change the length too much as changing the length of the graph’s data would mess up the error bars so I ended up manually placing the letters onto the graph with geom_text. To prove I got the same CLD letters as the paper, I calculated it with cldList.